package com.lagou.jdbc02;

import com.lagou.utils.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class TestDML {

    /*
    * 插入数据
    * */
    @Test
    public void testInsert() throws Exception{
         //1.通过JDBCUtils工具类 获取连接
        Connection connection= JDBCUtils.getConnection();

        //2.获取Statement对象
        Statement statement=connection.createStatement();

        //2.1编写SQL
        String str1="insert into jdbc_user values(null,'邵重德','123456','2000-07-10');";

        //2.2执行SQL
        int i=statement.executeUpdate(str1);
        System.out.println(i);

        //3.关闭流
        JDBCUtils.close(connection,statement);
    }

    /*
    *更新操作 根据id修改用户名
    * */
    @Test
    public void testUpdate() throws Exception{
        Connection connection=JDBCUtils.getConnection();
        Statement statement=connection.createStatement();
        String sql="update jdbc_user set username='刘能' where id=1;";
        int i=statement.executeUpdate(sql);
        JDBCUtils.close(connection,statement);
    }

    /*
    * 删除操作 删除id为1和id为2的数据
    * */
    @Test
    public void testDelete() throws Exception{
        Connection connection=JDBCUtils.getConnection();
        Statement statement=connection.createStatement();

        String sql="delete from jdbc_user where id in(6)";
        int i=statement.executeUpdate(sql);

        JDBCUtils.close(connection,statement);
    }
}
